package com.songlanyun.modules.userbankcard.controller.api;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.enums.MemberConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.utils.custom.SemaphoreUtils;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.userbankcard.entity.UserBankCardEntity;
import com.songlanyun.modules.userbankcard.model.dto.UserBankCardDTO;
import com.songlanyun.modules.userbankcard.service.UserBankCardService;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 会员的银行卡
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-12-08 13:56:16
 */
@RestController
@RequestMapping("api/member/bankcard")
public class UserBankCardApiController extends AbstractApiController {
    @Autowired
    private UserBankCardService userBankCardService;

    /**
     * 查询会员的所有银行卡数据
     */
    @GetMapping("/bankCardList")
    //@RequiresPermissions("userbankcard:userbankcard:list")
    public R bankCardList() {
        Long userId = getUserId(true);
        List<UserBankCardEntity> list = userBankCardService.bankCardList(MemberConstant.UserType.MEMBER, userId);
        list.forEach(UserBankCardEntity::loadDesCardNum);
        return R.ok().put("list", list);
    }

    /**
     * 获取会员默认银行卡
     */
    @GetMapping("/defaultBankCard")
    //@RequiresPermissions("userbankcard:userbankcard:info")
    public R defaultBankCard() {
        Long userId = getUserId(true);
        UserBankCardEntity userBankCard = userBankCardService.defaultBankCard(MemberConstant.UserType.MEMBER, userId);

        return R.ok().put("data", userBankCard);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("userbankcard:userbankcard:info")
    public R info(@PathVariable("id") Long id) {
        UserBankCardEntity userBankCard = userBankCardService.getById(id, true);

        return R.ok().put("data", userBankCard);
    }

    /**
     * 会员保存/修改银行卡信息
     */
    @RepeatSubmit(value = "2")
    @PostMapping("/save")
    //@RequiresPermissions("userbankcard:userbankcard:save")
    public R save(@RequestBody UserBankCardDTO dto) {
        ValidatorUtils.validateEntity(dto);
        userBankCardService.saveBankCard(dto, MemberConstant.UserType.MEMBER, getUserId(true));

        return R.ok();
    }


    /**
     * 会员设置默认银行卡
     */
    @RepeatSubmit(value = "2")
    @PostMapping("/setDefaultBankCard/{id}")
    //@RequiresPermissions("userbankcard:userbankcard:save")
    public R setDefaultBankCard(@PathVariable("id") Long id) {
        verifyUser(id);
        userBankCardService.setDefaultBankCard(MemberConstant.UserType.MEMBER, getUserId(true), id);

        return R.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("userbankcard:userbankcard:delete")
    public R delete(@PathVariable("id") Long id) {
        verifyUser(id);
        userBankCardService.removeById(id);

        return R.ok();
    }


    /**
     * 校验会员
     *
     * @param id
     */
    private void verifyUser(Long id) {
        UserBankCardEntity userBankCardEntity = userBankCardService.getById(id, true);

        if (!userBankCardEntity.getUserType().equals(MemberConstant.UserType.MEMBER)
                || !getUserId(true).equals(userBankCardEntity.getIdentityId())) {
            throw new RRException(MemberException.USER_EXCEPTION);
        }
    }


}
